﻿





$.fn.fadeable = function (options) {

    var settings = {
        target: this,
        duration: 'fast'
    };
    $.extend(settings, options)

    $(this).live('hover', function (event) {
        switch (event.type) {
            case 'mouseenter':
                $(this).find(settings.target).stop().fadeTo(settings.duration, 1);
                break;
            case 'mouseleave':
                $(this).find(settings.target).stop().fadeTo(settings.duration, 0);
                break;
        }
    });
}



function updateTips(t) {
    tips.text(t).addClass("ui-state-highlight");
    setTimeout(function () {
        tips.removeClass("ui-state-highlight", 1500);
    }, 500);
}

function checkLength(o, n, min, max) {
    if (o.val().length > max || o.val().length < min) {
        o.addClass("ui-state-error");
        updateTips("Length of " + n + " must be between " +
					min + " and " + max + ".");
        return false;
    } else {
        return true;
    }
}

function checkRegexp(o, regexp, n) {
    if (!(regexp.test(o.val()))) {
        o.addClass("ui-state-error");
        updateTips(n);
        return false;
    } else {
        return true;
    }
}

$(document).ready(function () {



    $("#logOutBtn").click(function () {


        window.location.replace("/logout");


    });

    var title = $("#title"),

			allFields = $([]).add(title),
			tips = $(".validateTips");


    $("#dialog-form").dialog({
        autoOpen: false,
        height: 320,
        width: 450,
        modal: true,
        buttons: {
            "Create board": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(title, "title", 3, 16);

                bValid = bValid && checkRegexp(title, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter.");




                var ntitle = $("#title").val();
                var ncategory = $("#categoryList option:selected").val();

                var request = $.ajax({
                    type: 'POST',
                    url: "/addboard",
                    dataType: "json",
                    data: { title: ntitle, category: ncategory }


                });
                request.done(function (data) {

                    if (data.redirect) {
                        // data.redirect contains the string URL to redirect to
                        window.location.replace(data.redirect);
                    }
                    else { 
                        
                    }



                });





            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#createNewBoard").click(function () {




        $("#dialog-form").dialog("open");
    });





    $('ol.dribbbles li div.dribbble-img').fadeable({ target: 'a.dribbble-over' });







});


              